package com.beagle.njddapp.ui.activity.owner;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.beagle.njddapp.R;
import com.beagle.njddapp.base.BaseMvpActivity;
import com.beagle.njddapp.bean.ZkBean;
import com.beagle.njddapp.bean.owner.OilOrderBean;
import com.beagle.njddapp.ui.fragment.CommonMapfragment;
import com.beagle.njddapp.ui.presenter.NjOilOrderDetailPresenter;
import com.beagle.njddapp.ui.view.NjOilOrderDetailView;
import com.beagle.njddapp.utils.StatusBarUtil;
import com.blankj.utilcode.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author hxx
 * @date 2021-03-22 20:21
 * 农机主-加油订单详情
 */
public class NjOilOrderDetailActivity extends BaseMvpActivity<NjOilOrderDetailView, NjOilOrderDetailPresenter> implements NjOilOrderDetailView {

    @BindView(R.id.commom_title_tv)
    TextView tvTitle;
    @BindView(R.id.common_container)
    RelativeLayout container;
    private long orderId;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_order_count)
    TextView tvCount;
    @BindView(R.id.tv_order_number)
    TextView tvOrderNumber;
    @BindView(R.id.tv_order_phone)
    TextView tvPhone;
    @BindView(R.id.tv_order_address)
    TextView tvAddrss;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_order_pay_time)
    TextView payTime;
    @BindView(R.id.tv_car_dirver)
    TextView tvCarName;
    @BindView(R.id.tv_order_pay_type)
    TextView tvPayType;
    @BindView(R.id.rl_pay_count)
    RelativeLayout rlPayType;
    @BindView(R.id.ll_zk)
    LinearLayout llZk;
    @BindView(R.id.rl_pay)
    RelativeLayout rlPay;
    @BindView(R.id.rl_pay_time)
    RelativeLayout rlPayTime;
    @BindView(R.id.tv_act)
    TextView tvAct;
    @BindView(R.id.ll_pj)
    LinearLayout llPj;
    @BindView(R.id.iv2)
    ImageView iv2;
    @BindView(R.id.iv3)
    ImageView iv3;
    @BindView(R.id.iv4)
    ImageView iv4;
    @BindView(R.id.iv5)
    ImageView iv5;
    @BindView(R.id.iv_pay)
    ImageView ivPay;
    private OilOrderBean oilOrderBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_nj_oilorder_detail;
    }

    @Override
    public void init() {
        StatusBarUtil.immersive(this, Color.WHITE);
        StatusBarUtil.darkMode(this);
        tvTitle.setText("订单详情");
        container.setBackgroundColor(Color.WHITE);
        orderId=getIntent().getLongExtra("orderId",-1);
        presenter.getOrderDetail(orderId);
    }

    private void addFragment() {

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); //去掉切换Fragment时闪动的动画效果
        CommonMapfragment commonMapfragment=new CommonMapfragment();
        Bundle bundle=new Bundle();
        if (!TextUtils.isEmpty(oilOrderBean.getMeshLatitude())){
            bundle.putDouble("latitude",Double.parseDouble(oilOrderBean.getMeshLatitude()));
            bundle.putDouble("longitude",Double.parseDouble(oilOrderBean.getMeshLongitude()));
        }
        commonMapfragment.setArguments(bundle);
        transaction.replace(R.id.map_container, commonMapfragment);
        transaction.commitNowAllowingStateLoss();
    }

    @OnClick({R.id.common_back_iv})
    public void onCLick(View view){
        switch (view.getId()){
            case R.id.common_back_iv:
                onBackPressed();
                break;

        }
    }


    @Override
    public NjOilOrderDetailView getMvpView() {
        return this;
    }

    @Override
    public NjOilOrderDetailPresenter createPresenter() {
        return new NjOilOrderDetailPresenter();
    }

    @Override
    public void onError(String message) {
        ToastUtils.showShort(message);
    }

    @Override
    public void showOrder(OilOrderBean result) {
        oilOrderBean=result;
        if (result==null){
            return;
        }
        addFragment();

        tvStatus.setVisibility(View.VISIBLE);
        if (oilOrderBean.isPayed()){
            tvStatus.setText("已完成");
            tvStatus.setTextColor(getResources().getColor(R.color.color_bule));
            tvStatus.setBackground(getResources().getDrawable(R.drawable.bg_oil_finish));
            if (oilOrderBean.getVoteRate()>0){
                llPj.setVisibility(View.VISIBLE);
                if (oilOrderBean.getVoteRate()==1){
                    iv5.setImageResource(R.mipmap.score_star_nor);
                    iv4.setImageResource(R.mipmap.score_star_nor);
                    iv3.setImageResource(R.mipmap.score_star_nor);
                    iv2.setImageResource(R.mipmap.score_star_nor);
                }else if (oilOrderBean.getVoteRate()==2){
                    iv5.setImageResource(R.mipmap.score_star_nor);
                    iv4.setImageResource(R.mipmap.score_star_nor);
                    iv3.setImageResource(R.mipmap.score_star_nor);
                }else if (oilOrderBean.getVoteRate()==3){
                    iv5.setImageResource(R.mipmap.score_star_nor);
                    iv4.setImageResource(R.mipmap.score_star_nor);
                }else if (oilOrderBean.getVoteRate()==4){
                    iv5.setImageResource(R.mipmap.score_star_nor);
                }
            }
        }else if ("canceled".equals(oilOrderBean.getOrderState())){
            tvStatus.setText("已取消");
            tvStatus.setTextColor(Color.parseColor("#58617A"));
            tvStatus.setBackground(getResources().getDrawable(R.drawable.bg_oil_cancle));
        }
        oilOrderBean=result;
        tvNumber.setText(result.getMeshNum());
        tvCount.setText(result.getMeter()+"L");
        tvCarName.setText(result.getTakerUserName());
        tvOrderNumber.setText(result.getOrderNum());
        tvPhone.setText(result.getTakerPhone());
        tvAddrss.setText(result.getMeshAddress());
        if (!result.isPayed()){
            rlPayType.setVisibility(View.GONE);
            rlPayTime.setVisibility(View.GONE);
        }else {
            tvAct.setText(result.getTotalAmount());
            presenter.getOrderInfo(orderId);
            rlPayType.setVisibility(View.VISIBLE);
            payTime.setText(result.getPayTime());
            tvStatus.setVisibility(View.GONE);
            tvPrice.setText(oilOrderBean.getPaymentTypeName());
            if (!TextUtils.isEmpty(oilOrderBean.getPaymentTypeName())){
                if(oilOrderBean.getPaymentTypeName().contains("支付宝")){
                    ivPay.setImageResource(R.mipmap.icon_alipay);
                }else if (oilOrderBean.getPaymentTypeName().contains("微信")){
                    ivPay.setImageResource(R.mipmap.icon_wxpay);
                }
            }
        }
    }

    @Override
    public void showZkInfo(ZkBean zkBean) {
        llZk.setVisibility(View.VISIBLE);
        rlPay.setVisibility(View.VISIBLE);
        tvAct.setText(zkBean.getActualPrice());
        String price=decimalFormat.format(Double.parseDouble(zkBean.getOilPrice())*Double.parseDouble(zkBean.getMeter()));
        tvPayType.setText(price+"元");

        if(!TextUtils.isEmpty(zkBean.getDiscountOneName())){
            addZk(zkBean.getDiscountOneName()+"-"+zkBean.getDiscountOneValue()+"%",zkBean.getDiscountOneCut());
        }
        if(!TextUtils.isEmpty(zkBean.getDiscountTwoName())){
            addZk(zkBean.getDiscountTwoName()+"-"+zkBean.getDiscountTwoValue()+"%",zkBean.getDiscountTwoCut());
        }

    }
    java.text.DecimalFormat decimalFormat =new java.text.DecimalFormat("#.##");

    public void addZk(String name,String price){
        View view=getLayoutInflater().inflate(R.layout.item_zk,null);
        TextView tvNmae=view.findViewById(R.id.title_tv);
        TextView tvCount=view.findViewById(R.id.tv_zk);
        tvNmae.setText(name);
        tvCount.setText("-"+decimalFormat.format(Double.parseDouble(price))+"元");
        llZk.addView(view);

    }
}
